package edu.uwlax.cs.oayonlinestore.client.gui.shop;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.FormTable;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.client.utils.VSpacer;
import edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO;

public class PageTrack extends TitlePage {
	String idsSaved;

	// int itemId;

	// int subID;

	// ProductDetailsVO prod;

	TextBox lastname, zip;

	// private DateEdit expDate;

	// TextArea desc;

	private class TrackCardTable extends DekoTable {

		Button delete;

		public TrackCardTable() {
			super();
			addTitle("ID");
			addTitle("Date");
			addTitle("Status");

		}

		public void addItem(OrderSummaryVO gv) {

			AddRow r = addRow(0);
			r.add(gv.getId());
			String date = gv.getOrderDate() + "";
			if (date.equals("null"))
				date = "Order not submitted yet.";
			r.add(date);
			r.add(gv.getStatus());
		}

	}

	// _____________

	private class ItemForm extends FormTable {
		public ItemForm() {
			super();
			
			addInput("Last Name", lastname = new TextBox());
			addInput("ZIP code", zip = new TextBox());

			addButton("Track Orders", null);
			//addInput(new VSpacer("4px"));
		}

		public void onButtonClicked() {
			int zipcode = 0;
			try {
				zipcode = Integer.parseInt(zip.getText());
			} catch (NumberFormatException e) {
				Window.alert("Please enter valid ZIP code !");
			}

			OnlinestoreRService.Util.getInstance().getOrdersByLastNameAndZip(
					lastname.getText(), zipcode, new TrackAsycHandler());

		}

		public void onDataLoad(Object data) {
		}
	}

	ItemForm table;

	TrackCardTable orderTable;

	protected void create() {
		setTitle("Track Orders");
		add(table = new ItemForm());
		add(new VSpacer("4px"));
		add(orderTable = new TrackCardTable());

	}

	public class TrackAsycHandler extends DefaultAsyncCallback {

		public void onSuccess(Object o) {
			removeAll();
			setTitle("Track Orders");
			add(table = new ItemForm());
			add(orderTable = new TrackCardTable());
			OrderSummaryVO[] orders = (OrderSummaryVO[]) o;

			for (int i = 0; i < orders.length; i++) {
				orderTable.addItem(orders[i]);

			}

			// onShow();

		}
	}

	public void onShow() {

		removeAll();
		setTitle("Track Orders");
		add(table = new ItemForm());
		add(new VSpacer("10px"));
		add(orderTable = new TrackCardTable());
		// return true;

	}

	public String getID() {
		return "TrackOrder";
	}
}
